// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.webmanager;

import com.google.zahra.testmanager.TestManager;
import com.google.zahra.util.Util;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author arthurkok@google.com (Your Name Here)
 *
 */

public class SubmitTestServlet extends HttpServlet {
  
  static final TestManager testManager = WebManager.TEST_MANAGER;
  
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String jobIdString = request.getParameter("jobId");
    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    StringBuffer bodyString = new StringBuffer();    
    
    bodyString.append("<h2>Submit a test</h2>");
   
    bodyString.append(
            "<FORM ACTION='/submitTest' METHOD='POST'>" + "<TABLE>"
                + "<TR><TH id='submit'>Name</TH><TD><INPUT TYPE='TEXT' NAME='name'></TD></TR>"                
                + "<TR><TH id='submit'>Os</TH><TD>"
                + "Windows:<input type='checkbox' name='windows' value='1' /> "
                + "Mac:<input type='checkbox' name='mac' value='2' /> "
                + "Unix:<input type='checkbox' name='unix' value='4' /></TD></TR>"                
                + "<TR><TH id='submit'>Retries</TH><TD><INPUT TYPE='TEXT' NAME='retries' VALUE='2'></TD></TR>"
                + "<TR><TH id='submit'>Python Script</TH><TD><TEXTAREA ROWS=15 COLS=80 NAME='pythonFile'></TEXTAREA></TD></TR>" 
                + "<TR><TH id='submit'>Number of Tests to Add</TH><TD><INPUT TYPE='TEXT' NAME='numOfTest' VALUE='1'></TD></TR>" 
                + "<TR><TD></TD><TD><INPUT TYPE='SUBMIT' VALUE='Submit Test'></TD></TR>" 
                + "</TABLE>" + "</FORM>");    

    response.getWriter().print(Util.getHTML(bodyString.toString(), "Zahra - Submit new Test"));
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    
    StringBuffer bodyString = new StringBuffer();

    int numOfTest = Integer.parseInt(request.getParameter("numOfTest"));
    
    int os = 0;
    
    if ( request.getParameter("windows") != null ) {
      os = os | 1;
    }
    if ( request.getParameter("mac") != null ) {
      os = os | 2;
    }
    if ( request.getParameter("unix") != null ) {
      os = os | 4;
    }
    
    
    int retries = Integer.parseInt(request.getParameter("retries"));

    for (int x = 0; x < numOfTest; x++) {
      String name = request.getParameter("name");
      testManager.addTestCase(request.getParameter("pythonFile"), name, os, retries);
    }

    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    bodyString.append("Added " + numOfTest + " of tests successfully!");    

    response.getWriter().print(Util.getHTML(bodyString.toString(), "Zahra - Submit new Test"));
  }
}

